package com.demo.android.bmi_mvc;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Bmi_mvc extends Activity {
	
	public static final String PREF = "BMI_PREF";
	public static final String PRE_HEIGHT = "BMI_Height";
	
	public static final String TAG = "LogBMI";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.v(TAG, "onCreate");
		setContentView(R.layout.activity_bmi_mvc);
		
		findViews();
		setListeners();
		restorePrefs();
		//listen for button clicks
		//Button button = (Button)findViewById(R.id.submit);
		//button.setOnClickListener(calcBMI);
		
	}
	
	private Button calcbutton;
	private EditText fieldweight;
	private EditText fieldheight;
	private TextView view_result;
	private TextView view_suggest;
	
	private void findViews(){
		calcbutton = (Button)findViewById(R.id.submit);
		fieldweight = (EditText)findViewById(R.id.weight);
		fieldheight = (EditText)findViewById(R.id.height);
		view_result = (TextView)findViewById(R.id.result);
		view_suggest = (TextView)findViewById(R.id.suggest);
	}
	
	private void setListeners(){
		calcbutton.setOnClickListener(calcBMI);
	}
	
	//实现接口
	private Button.OnClickListener calcBMI = new Button.OnClickListener(){
		
		@Override
		public void onClick(View v){
			
			try {
				/*
			DecimalFormat nf = new DecimalFormat("0.00");
			//EditText fieldheight = (EditText)findViewById(R.id.height);
			//EditText fieldweight = (EditText)findViewById(R.id.width);
			double height = Double.parseDouble(fieldheight.getText().toString())/100;
			double weight = Double.parseDouble(fieldweight.getText().toString());
			double BMI = weight/(Math.pow(height, 2));
			
			//TextView result = (TextView)findViewById(R.id.result);
			view_result.setText("Your BMI is "+nf.format(BMI));
			
			//TextView fieldsuggest = (TextView)findViewById(R.id.suggest);
			
			  if (BMI>25){
				  view_suggest.setText(R.string.advice_heavy);
			   } else if (BMI<20){
				  view_suggest.setText(R.string.advice_light);
			   } else {
				  view_suggest.setText(R.string.advice_average);
			     }
			  //openOptionsDialog();
			   * 
			   */
			  Intent intent = new Intent();
			  intent.setClass(Bmi_mvc.this, Report.class);
			  Bundle bundle = new Bundle();
			  bundle.putString("KEY_HEIGHT", fieldheight.getText().toString());
			  bundle.putString("KEY_WEIGHT", fieldweight.getText().toString());
			  intent.putExtras(bundle);
			  
			  startActivity(intent);
			}//end try
			
			catch(Exception e){
				//Toast.makeText(getApplicationContext(), R.string.input_error, Toast.LENGTH_SHORT).show();
			}
		}
	};//接口已实现
	
	private void openOptionsDialog(){
		/*
		new AlertDialog.Builder(Bmi_mvc.this)
		.setTitle(R.string.about_title)
		.setMessage(R.string.about_msg)
		.show();
		*/
		//AlertDialog.Builder dialog = new AlertDialog.Builder(Bmi_mvc.this);
		Builder dialog = new Builder(Bmi_mvc.this);
		dialog.setTitle(R.string.about_title);
		dialog.setMessage(R.string.about_msg);
		dialog.setPositiveButton(R.string.ok_label,
				new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialogInterface, int which) {
						// TODO Auto-generated method stub
						
					}
				}
		);
		dialog.setNegativeButton(R.string.homepage_label,
				new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						Uri uri = Uri.parse("http://www.qq.com/");
						Intent intent = new Intent(Intent.ACTION_VIEW,uri);
						startActivity(intent);
					}
				});
		
		dialog.setNeutralButton(R.string.map_label,
				new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						Uri uri = Uri.parse(getString(R.string.gps_geo));
						Intent intent = new Intent(Intent.ACTION_VIEW,uri);
						startActivity(intent);
						
					}
				});
		dialog.show();
		//Toast.makeText(getApplicationContext(), "弹窗这里", Toast.LENGTH_SHORT).show();
	}
	
	protected static final int MENU_ABOUT = Menu.FIRST;
	protected static final int MENU_Quit = Menu.FIRST + 1;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.bmi_mvc, menu);
		super.onCreateOptionsMenu(menu);
		menu.add(0, MENU_ABOUT, 0, "关于").setIcon(R.drawable.help_browser);
		menu.add(0,MENU_Quit,0,"结束").setIcon(R.drawable.emblem_unreadable);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		super.onOptionsItemSelected(item);
		switch (item.getItemId()){
		    case MENU_ABOUT:
		    	openOptionsDialog();
		    	break;
		    case MENU_Quit:
		    	finish();
		    	break;
		}
		return true;
		
	}
	
	private void restorePrefs(){
		SharedPreferences settings = getSharedPreferences(PREF,0);
		String pref_height = settings.getString(PRE_HEIGHT, "");
		if (!"".equals(pref_height)){
			fieldheight.setText(pref_height);
			fieldweight.requestFocus();
		}
	}
	
	@Override
	protected void onPause(){
		super.onPause();
		Log.v(TAG, "onPause");
		SharedPreferences settings = getSharedPreferences(PREF,0);
		settings.edit().putString(PRE_HEIGHT, fieldheight.getText().toString())
		.commit();
	}
	
	public void onRestart(){
		super.onRestart();
		Log.v(TAG, "onRestart");
	}
	
	public void onStart(){
		super.onStart();
		Log.v(TAG, "onStart");
	}
	
	public void onStop(){
		super.onStop();
		Log.v(TAG, "onStop");
	}
	
	public void onResume(){
		super.onResume();
		Log.v(TAG, "onResume");
	}
	
	public void onDestroy(){
		super.onDestroy();
		Log.v(TAG, "onDestroy");
	}
}
